﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MakeBorderless
{
    internal static class Program
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            bool ranConsole = false;
            Settings s = null;
            Console.Title = "Make Borderless";
            if (args.Length > 0)
            {
                s = ParseArgs(args);
                ranConsole = true;
            }
            setConsoleWindowVisibility(false, Console.Title);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain(s, ranConsole));
            setConsoleWindowVisibility(true, Console.Title);
        }

        private static void setConsoleWindowVisibility(bool visible, string title)
        {
            IntPtr hWnd = FindWindow(null, title);

            if (hWnd != IntPtr.Zero)
            {
                if (!visible)
                {
                    //Hide the window
                    ShowWindow(hWnd, 0); // 0 = SW_HIDE
                }

                else
                {
                    //Show window again
                    ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
                }
            }
        }

        private static Settings ParseArgs(string[] args)
        {
            Settings s = new Settings();
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "--minimized" || args[i] == "-m")
                {
                    s.RunMinimized = true;
                }

                else if (args[i] == "--window" || args[i] == "-w")
                {
                    s.GroupBy = GroupType.UseWindowName;
                }

                else if (args[i] == "--process" || args[i] == "-p")
                {
                    s.GroupBy = GroupType.UseProcessName;
                }

                else if (args[i] == "--help" || args[i] == "-h" || args[i] == "-?")
                {
                    Console.WriteLine("Make Borderless Commands:");
                    Console.WriteLine("=========================" + Environment.NewLine);
                    Console.WriteLine("  --help  or -h  or -?    : Displays this list of commands");
                    Console.WriteLine("  --minimized    or -m    : Runs the application minimized on start.");
                    Console.WriteLine("  --window       or -w    : Groups windows by window name in the TreeView.");
                    Console.WriteLine("  --process      or -p    : Grpups windows by process name in the TreeView.");
                    Console.WriteLine("  --names []     or -n [] : Names to set to borderless on start.");
                    Console.WriteLine("                            > Example: -n \"name1 name2 ...\"" + Environment.NewLine);
                    Console.Write("Press enter key to continue...");
                    Console.ReadKey(true);
                    Environment.Exit(0);
                }
                else if (args[i] == "--names" || args[i] == "-n")
                {
                    if ((i + 1) <= (args.Length - 1))
                    {
                        foreach (string n in args)
                        {
                            if (!IsCmd(n))
                            {
                                s.SavedNames.Add(n);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return s;
        }

        private static bool IsCmd(string var)
        {
            switch (var)
            {
                case "-m":
                    return true;
                case "--minimized":
                    return true;
                case "-w":
                    return true;
                case "--window":
                    return true;
                case "-p":
                    return true;
                case "--process":
                    return true;
                case "-h":
                    return true;
                case "-?":
                    return true;
                case "--help":
                    return true;
                default:
                    return false;
            }
        }
    }
}